<?php  
/*
$Id$
*/


if (!defined('BASEPATH')) exit('No direct script access allowed');

class Album extends Controller {

	function Album()
	{
		parent::Controller();
		$this->template['module'] = "album";
		$this->load->model("album_model", "album");
		if(!isset($this->album->album_settings))
		{
			echo __("This module is not yet set up.", "album")  . "<br />";
			echo anchor('admin/album/settings', __("Go here to set it up", "album"));
			exit();
		}
		
		__("Everybody", "album");
		__("Members Only", "album");
	}
	
	
	function index()
	{
		$this->lisitra();
	}

	//lisitry ny tahirintsary
	function lisitra( $start = 0)
	{
		$per_page = 20;
		$params['start'] = $start;
		$params['limit'] = $per_page;
		$params['order_by'] = "id DESC";
		$params['where'] = "a.username = '" . $this->user->username . "' OR g_id IN ('" . implode("','" , $this->user->groups) . "')" ;
		$this->template['rows'] = $this->album->get_albums($params);

		$this->template['title'] = __("Album list", "album");
		$config['uri_segment'] = 3;
		$config['first_link'] = __('First', 'album');
		$config['last_link'] = __('Last', 'album');
		$config['base_url'] = base_url() . 'album/lisitra';
		$config['total_rows'] = $this->album->get_total_albums($params);
		$config['per_page'] = $per_page; 
		$config['num_links'] = 10;

		$this->load->library('pagination');
		
		$this->pagination->initialize($config); 

		$this->template['pager'] = $this->pagination->create_links();		
		$this->template['start'] = $start;		
		$this->template['total'] = $config['total_rows'];		
		$this->template['per_page'] = $config['per_page'];		
	
		
		$this->layout->load($this->template, 'album/index');
		
	}
	
	function asehoy( $aid = null, $start = 0)
	{
		
		
		if (is_null($aid))
		{
			$this->session->set_flashdata('notification', __("You didn't choose any album", "album"));
			redirect('album/lisitra');
			return;
		}
		
		if($row = $this->album->get_album(array('where' => array('a.aid' => $aid, ))))
		{
			
			//until groups are settled
			if($row['g_id'] != '0')
			{
				$this->user->require_login();
			}
			if ($row['username'] != $this->user->username && !in_array($row['g_id'], $this->user->groups))
			{
				$this->template['title'] = __("Access error", "album");
				$this->template['message'] = __("You are not allowed by the owner of the album to view it", "album");
				$this->layout->load($this->template, 'error');
				return;
			}
			
			
			
			$per_page = 24;
			$this->template['title'] = $row['title'];
			$this->template['row'] = $row;
			
			$params['start'] = $start;
			$params['limit'] = $per_page;
			$params['order_by'] = "album_images.id DESC";
			$params['where'] = array('album_images.aid' => $aid);
			
			
			
			$sary = $this->album->get_images($params);
			$this->template['sary'] = $sary;
	
			if (!$row['cover'])
			{
				$row['cover'] = $sary['0']['s'];
				$this->album->update_album($row['aid'], array('cover' => $this->db->escape($row['cover'])));
			}
			
			$this->load->library('pagination');
			$config['uri_segment'] = 4;
			$config['first_link'] = __('First', "album");
			$config['last_link'] = __('Last', "album");
			$config['base_url'] = base_url() . 'album/asehoy/' . $aid;
			$config['total_rows'] = $this->album->get_total_images($params);
			$config['per_page'] = $per_page; 
			$config['num_links'] = 10;

			$this->pagination->initialize($config); 

			$this->template['pager'] = $this->pagination->create_links();		

			if ($this->session->userdata('count_album_'.$row['id']) != $row['id'])
			{
				$this->session->set_userdata('count_album_'.$row['id'], $row['id']);
				$this->album->update_album($row['aid'], array('view' => 'view+1'));
			}
			
			
			$this->layout->load($this->template, 'album/asehoy');
		}
		else
		{
			$this->session->set_flashdata('notification', __("Album not found", "album"));
			redirect('album');
		}
		
	}
	
	function user($username = null, $start = 0)
	{
		if(is_null($username))
		{
			redirect ('album');
			return;
		}
		$per_page = 20;
		
		
		$this->load->library('pagination');
		
		$params['start'] = $start;
		$params['per_page'] = $per_page;
		$params['order_by'] = "id DESC";
		$params['where'] = "a.username = '$username' AND (a.g_id IN ('" . implode("','" , $this->user->groups) . "') OR a.username='" . $this->user->username . "')" ;
		$this->template['rows'] = $this->album->get_albums($params);
		$this->template['title'] = sprintf(__("Album that belong to %s", "album"), $username);
		$config['uri_segment'] = 4;
		$config['first_link'] = __('First', 'album');
		$config['last_link'] = __('Last', 'album');
		$config['base_url'] = base_url() . 'album/user/' . $username;
		$config['total_rows'] = $this->album->get_total_albums($params);
		$config['num_links'] = 10;
		
		$config['per_page'] = $per_page; 

		$this->pagination->initialize($config); 

		$this->template['pager'] = $this->pagination->create_links();		
		$this->template['start'] = $start;		
		$this->template['total'] = $config['total_rows'];		
		$this->template['per_page'] = $config['per_page'];		
	
		
		$this->layout->load($this->template, 'album/index');
		
	}
	
	function tag($tag = null, $start = 0)
	{
		if(is_null($tag))
		{
		
			$this->template['title'] = __("Tag list", "album");
			
			$this->template['rows'] = $this->album->get_tags();
			
			$this->layout->load($this->template, 'tags');
			
			return;
		}
		
		$per_page = 20;
		
		
		$this->load->library('pagination');
		
		$params['start'] = $start;
		$params['per_page'] = $per_page;
		$params['order_by'] = "id DESC";
		$this->template['rows'] = $this->album->get_album_by_tag($tag, $start, $per_page);
		
		$this->template['title'] = __("Album with the spicified tag", "album");
		$config['uri_segment'] = 4;
		$config['first_link'] = __('First', 'album');
		$config['last_link'] = __('Last', 'album');
		$config['base_url'] = base_url() . 'album/tag/' . $tag;
		$config['total_rows'] = $this->album->get_total_album_by_tag($tag);
		$config['per_page'] = $per_page; 
		$config['num_links'] = 10;
		
		$this->pagination->initialize($config); 

		$this->template['pager'] = $this->pagination->create_links();		
		$this->template['start'] = $start;		
		$this->template['total'] = $config['total_rows'];		
		$this->template['per_page'] = $config['per_page'];		
	
		
		$this->layout->load($this->template, 'album/index');
	
		
	}
	function ampidiro($aid = null)
	{
		$this->template['title'] = __("Create an album", $this->template['module']);
		
		if (isset($this->album->album_settings->user_can_create) && $this->album->album_settings->user_can_create == 1)
		{
			$this->user->require_login();
		}
		else
		{
			$this->user->check_level('album', LEVEL_ADD) ;
		}
		$this->template['tags'] = $this->album->get_tags(array('limit' => 40, 'order_by' => 'cnt DESC' ));
		
		
		if ($this->input->post('submit'))
		{
			if (!$this->input->post('title'))
			{
				$this->template['error'][] = __("Title is required.", "album");
				$this->template['row'] = $_POST;
				$this->layout->load($this->template, 'ampidiro');
				return;
			}
			
			if ($aid = $this->input->post('aid'))
			{
				$row = $this->album->get_album(array('where' => array('a.aid' => $aid)));
				if(($this->user->level['album'] < LEVEL_EDIT) && ($this->user->username != $row['username']))
				{
					$this->template['error'][] = __("You cannot edit this album", "album") ;
					$this->template['row'] = $_POST;
					$this->layout->load($this->template, 'ampidiro');
					return;
				}

				$aid = $this->album->save_album();
				$this->session->set_flashdata('notification', __("Album updated", "album"));
				
				redirect("album/asehoy/" . $aid);
				
			}
			else
			{

				$aid = $this->album->save_album();
				$this->plugin->do_action('album_save_album', $aid);
				$this->session->set_flashdata('notification', __("Album saved", "album"));
				redirect("album/asehoy/" . $aid);
				
			}
		}
		
		

		$row = array();
		
		if ($last_post = $this->session->userdata("last_post"))
		{
			$this->template['row'] = $last_post;
			$this->session->set_userdata("last_post", array());
		}
		$this->template['groups'] = $this->user->get_group_list();
		if (!is_null($aid))
		{
			$this->template['title'] = __("Modify the album", "album");
			$row = $this->album->get_album(array('limit' => null, 'where' => array('a.aid' => $aid)));
			if($row['username'] != $this->user->username)
			{
				//check only if not the submitter
				$this->user->check_level($this->template['module'], LEVEL_EDIT);
			}
			$this->template['row'] = $row;
			$this->template['sary'] = $this->album->get_images(array('where' => array('album_images.aid' => $row['aid'])));
			$this->template['groups'] = $this->user->get_group_list(array('where' => array('g_owner' => $row['username'])));
		}
		

		$this->layout->load($this->template, 'album/ampidiro');
	
	}
	
	function fafao( $aid , $js = 0)
	{
		$this->user->require_login();
		$this->template['title'] = __("Delete an album", "album");
		if (is_null($aid))
		{
			$this->session->set_flashdata("notification", __("Please choose an album.", "album"));
			
			redirect("album/user/" . $this->user->username);
			return;
		}
		
		if ($this->album->get_images(array('where' => array('album_images.aid' => $aid))))
		{
			$this->session->set_flashdata("notification", __("That album is not empty, please delete all images in it.", "album"));
			
			redirect("album/ampidiro/" . $aid);
		}
		
		
		if ($js == 0)
		{
			$this->template['row'] = $this->album->get_album(array('where' => array('a.aid' => $aid)));
			$this->layout->load($this->template, "album/delete");
			return;
		}
		
		$row = $this->album->get_album(array('where' => array('a.aid' => $aid)));
		
		if($row['username'] != $this->user->username)
		{
			//check only if not the submitter
			$this->user->check_level($this->template['module'], LEVEL_DEL);
		}
		
		if ($this->album->delete($aid))
		{
			$this->session->set_flashdata("notification", __("Album deleted"));
			redirect("album/user/" . $this->user->username);
		}
		else
		{
			$this->session->set_flashdata("notification", __("Unknown error.", "album"));
			
			redirect("album/user/" . $this->user->username);
		}
	}

	
}